package com.threeti.mecool.core.domain.model.sellinto;

import com.threeti.mecool.core.domain.model.acl.User;
import com.threeti.mecool.core.domain.model.project.ProjectStore;
import com.threeti.mecool.framework.domain.support.DefaultDomainEntity;
import com.threeti.mecool.framework.util.Lists;
import com.threeti.mecool.legacy.domain.model.basicdata.biz.PosKindLegacy;
import com.threeti.mecool.legacy.domain.model.basicdata.biz.PosSysTypeLegacy;
import com.threeti.mecool.legacy.infrastructure.persistence.PosKindLegacyRepository;
import com.threeti.mecool.legacy.infrastructure.persistence.PosSysTypeLegacyRepository;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * 卖进<p>
 * <p/>
 * 1.卖进一定是针对特定项目特定门店的<br/>
 * 2.卖进不必一次执行完成，可以连续执行，但针对特定项目特定门店，系统中只会存在唯一一条卖进记录，后续的卖进资料录入都是针对同一条记录<br/>
 * 3.卖进操作不直接影响其它业务，虽然在逻辑上它属于前置型业务
 *
 * @author Jay Meng
 */
@Configurable
@Entity
@Table(name = "DM_SELLINTO")
public class SellInto extends DefaultDomainEntity {

    @ManyToOne
    @JoinColumn(name = "USER_ID")
    private User user;

    @OneToOne(optional = false)
    private ProjectStore projectStore;

    @Enumerated(EnumType.ORDINAL)
    private SellIntoStatus status;

    @Enumerated(EnumType.ORDINAL)
    private TGStockBaseDesc tgStockBaseDesc;

    @Enumerated(EnumType.ORDINAL)
    private SellIntoExecutionStatus executionStatus;

    @Embedded
    private SellIntoProblem problem;

    private String description;

    private Date lastModified = new Date();

    @ElementCollection(targetClass = SalesPromoter.class, fetch = FetchType.EAGER)
    @CollectionTable(name = "DM_SELLINTO_SALES_PROMOTER")
    private List<SalesPromoter> salesPromoters = Lists.newArrayList();

    @Autowired
    @Transient
    @JsonIgnore
    private PosKindLegacyRepository posKindLegacyRepository;

    @Autowired
    @Transient
    @JsonIgnore
    private PosSysTypeLegacyRepository posSysTypeLegacyRepository;

    public SellInto(User user, ProjectStore projectStore) {
        this.user = user;
        this.projectStore = projectStore;
    }

    /**
     * 实施卖进的用户(一般即指督导)
     */
    public User getUser() {
        return user;
    }

    /**
     * 实施卖进的项目门店(卖进一定是针对特定项目特定门店的)
     */
    public ProjectStore getProjectStore() {
        return projectStore;
    }

    /**
     * 取得(门店/终端)卖进情况(状态)
     */
    public SellIntoStatus getStatus() {
        return status;
    }

    /**
     * 取得卖进操作的最后一次执行时间
     */
    public Date getLastModified() {
        return lastModified;
    }

    /**
     * 取得堆头情况
     */
    public TGStockBaseDesc getTgStockBaseDesc() {
        return tgStockBaseDesc;
    }

    /**
     * 取得卖进执行状态
     */
    public SellIntoExecutionStatus getExecutionStatus() {
        return executionStatus;
    }

    /**
     * 取得卖进备注
     */
    public String getDescription() {
        return description;
    }

    /**
     * 取得卖进问题
     */
    public SellIntoProblem getProblem() {
        return problem;
    }

    /**
     * 取得促销员集合
     */
    public List<SalesPromoter> getSalesPromoters() {
        return salesPromoters;
    }

    @JsonIgnore
    @Transient
    public String getCategoryName() {
        PosKindLegacy posKindLegacy = posKindLegacyRepository.findOne(projectStore.getChannel().getCategory());
        return posKindLegacy != null ? posKindLegacy.getName() : "";
    }

    @JsonIgnore
    @Transient
    public String getSysCategoryName() {
        PosSysTypeLegacy posSysTypeLegacy = posSysTypeLegacyRepository.findOne(projectStore.getChannel().getSysCategory());
        return posSysTypeLegacy != null ? posSysTypeLegacy.getName() : "";
    }

    @Override
    public String toString() {
        return "SellInto [user=" + user + ", projectStore=" + projectStore + ", status=" + status + ", tgStockBaseDesc="
                + tgStockBaseDesc + ", executionStatus=" + executionStatus + ", problem=" + problem + ", description="
                + description + ", lastModified=" + lastModified + ", salesPromoters=" + salesPromoters + "]";
    }

    public SellInto() {
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setProjectStore(ProjectStore projectStore) {
        this.projectStore = projectStore;
    }

    public void setStatus(SellIntoStatus status) {
        this.status = status;
    }

    public void setTgStockBaseDesc(TGStockBaseDesc tgStockBaseDesc) {
        this.tgStockBaseDesc = tgStockBaseDesc;
    }

    public void setExecutionStatus(SellIntoExecutionStatus executionStatus) {
        this.executionStatus = executionStatus;
    }

    public void setProblem(SellIntoProblem problem) {
        this.problem = problem;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setLastModified(Date lastModified) {
        this.lastModified = lastModified;
    }

    public void setSalesPromoters(List<SalesPromoter> salesPromoters) {
        this.salesPromoters = salesPromoters;
    }
}
